// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import '../static/style/common.less'
import VueRouter from 'vue-router'
import router from './router'
import axios from 'axios'
// 初始化全局http回调
import {init, fetch} from './common/request.js';
window.ajax = fetch;
window.axios=axios
Vue.prototype.axios = axios
import { Button,NavBar,Field,Cell, CellGroup,Toast,Picker,Dialog,Actionsheet,Uploader,Icon,Row,Col, PullRefresh} from 'vant';
Vue.use(VueRouter)
Vue.use(Button)
Vue.use(NavBar)
Vue.use(Field)
Vue.use(Cell)
Vue.use(CellGroup)
Vue.use(Toast)
Vue.use(Picker)
Vue.use(Dialog)
Vue.use(Actionsheet)
Vue.use(Uploader)
Vue.use(Icon)
Vue.use(Row)
Vue.use(Col)
Vue.use(PullRefresh)


Vue.config.productionTip = false

router.beforeEach(function (to,from,next) {

    if(sessionStorage.getItem('user')){
      next()
    }else{
      if(to.query.code){
        axios.get('/api/login',{params:{code:to.query.code}}).then(res=>{
          if(res.data.err_code==0){
            sessionStorage.setItem('user',JSON.stringify(res.data.data))
            next()
          }else{
            let returnUrl = encodeURI(window.location.href);
            // let url = 'http%3a%2f%2fm.zxskill.com'
            window.location.href='https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx838cf54dd030d93a&redirect_uri='+returnUrl+'&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect'
          }
          next()
        }).catch(err=>{
          next()
        })

      }else{
        let returnUrl = encodeURI(window.location.href);
        // let url = 'http%3a%2f%2fm.zxskill.com'
        window.location.href='https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx838cf54dd030d93a&redirect_uri='+returnUrl+'&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect'
      }
    }

})



/* eslint-disable no-new */
const vm = new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
